#!/bin/bash

# Note : Make sure you place NOPASSWD in your /etc/sudoers or this script will annoy you (see jfconfig -> fix stuff)

function usage() {
  echo usage : jfpac output-file-version.pkg.tar.xz arch [files.lst]
  echo desc : creates PAC package
  echo arch : any, x32, x64, a32, a64
  exit
}

function error() {
  echo Error : $1
  exit
}

if [ "$1" = "" ]; then usage; fi
if [ "$1" = "--help" ]; then usage; fi
if [ "$1" = "-h" ]; then usage; fi

FILES=files.lst
ARCH=$2
BITS=${ARCH:1:2}
if [ -f files-$BITS.lst ]; then FILES=files-$BITS.lst; fi

if [ ! "$3" = "" ]; then FILES=$3; fi

java -cp /usr/share/java/javaforce.jar javaforce.utils.GenPkgInfo arch $ARCH $FILES

echo .PKGINFO>.files.tmp
echo .MTREE>>.files.tmp
cat $FILES>>.files.tmp

#build .MTREE
sudo bsdtar -czf .MTREE --format=mtree --options='!all,use-set,type,uid,gid,mode,time,size,md5,sha256,link' -T $FILES
sudo tar cf $1 -T .files.tmp

rm .PKGINFO
rm .MTREE
rm .files.tmp

echo Complete!
